from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_new_database_dialog import Ui_Dialog_new_database
from database.db import Database
from app.window.msg_box import Msg_box
import os
from app.data import init_database


class New_datbase_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(New_datbase_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_new_database()
        self.ui.setupUi(self)

        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮
        self.msg = Msg_box(self)
        self.ui.pushButton_ok.clicked.connect(self.slot_ok)

    def slot_ok(self):
        name = self.ui.lineEdit_database_name.text()
        if name.strip() == '':
            self.msg.msg_tips('数据库名称不能为空!')
            return

        file_path = './database/' + name + '.db'
        os.makedirs(os.path.dirname(file_path),exist_ok=True)
        with open (file_path,'w') as db_file:
            # 初始化数据库
            init_database(file_path)
            self.msg.msg_tips('数据库文件创建成功!')
            self.hide()

            

